/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Datos;

import Entidades.Color;
import Interfaces.IColor;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HOUSE
 */
public class DBColor implements IColor {

    private Connection conexion = null;

    public Connection getConection() {
        if (conexion == null) {
            try {
                Driver driver = new com.mysql.jdbc.Driver();
                DriverManager.registerDriver(driver);
                conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/automotora", "root", "pass1234");
            } catch (SQLException ex) {
                ex.printStackTrace();
                Logger.getLogger(DBAutos.class.getName()).log(Level.SEVERE, null, ex);

            }
        }
        return conexion;
    }

    @Override
    public boolean IngresaColor(Color nodo) {
        try {
            CallableStatement statement;
            statement = getConection().prepareCall("{call IngresaColor (?,?)}");
            statement.setInt(1, nodo.getIDColor());
            statement.setString(2, nodo.getNombre());



            statement.executeUpdate();
        } catch (Exception ex) {
            System.out.println("Error al agregar color");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean ModificaColor(Color nodo) {
        try {
            CallableStatement statement;
            statement = getConection().prepareCall("{call ModificaColor (?,?)}");
            statement.setInt(1, nodo.getIDColor());
            statement.setString(2, nodo.getNombre());





            statement.executeUpdate();
        } catch (Exception ex) {
            System.out.println("Error al modificar Color");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean EliminaColor(int IDColor) {
        try {
            CallableStatement statement;
            statement = getConection().prepareCall("{call EliminaColor(?)}");
            statement.setInt(1, IDColor);
            statement.executeUpdate();


        } catch (Exception ex) {
            System.out.println("Error al eliminar Color");
            ex.printStackTrace();
            return false;
        }
        return true;

    }

    @Override
    public ArrayList<Color> ListaColor() {
        ArrayList<Color> per = new ArrayList<Color>();
       try {
           CallableStatement statement = getConection().prepareCall("{call ListaColor}");
           ResultSet resultSet = statement.executeQuery();
           while (resultSet.next()) {
               Color reglam= new Color();
              
               reglam.setIDColor(resultSet.getInt(1));
               reglam.setNombre(resultSet.getString(2));
               
               
               per.add(reglam);
              
           }
           return per;
       } catch (SQLException ex) {
           ex.printStackTrace();
           return null;
       }
    }

    @Override
    public DefaultTableModel ModeloColor() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
